package net.yebaihe.wc;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

import android.graphics.Bitmap;

public abstract class DataMan {

	enum MSGTYPE {LOGIN, REGISTER,UPDATE_IMAGE};
	
	private static DataMan self=null;
	protected StateMachine machine;
	public static DataMan get(StateMachine machine) {
		if (self==null){
			self=new OfflineDataMan();
		}
		self.machine=machine;
		return self;
	}
	public void login(String email, String passwd) {
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("email", email);
		map.put("passwd", passwd);
		SendNormalRequest(MSGTYPE.LOGIN,map);
	}
	protected abstract void SendNormalRequest(MSGTYPE login, HashMap<String, String> params);
	public void register(String email, String username, String passwd) {
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("email", email);
		map.put("username", username);
		map.put("passwd", passwd);
		SendNormalRequest(MSGTYPE.REGISTER,map);
	}
	public void updateCurrUserImageInfo(Bitmap bmp) {
		final ByteArrayOutputStream os = new ByteArrayOutputStream();  
		bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
		HashMap <String,String>map=new HashMap <String,String>();
		map.put("email", self.machine.getWCAppContext().email);
		map.put("image", Base64.encodeBytes(os.toByteArray()));
		SendNormalRequest(MSGTYPE.UPDATE_IMAGE,map);
	}

}
